import moment from 'moment'
import chinese from './chinese'
import {festival, holiday} from './festival'

/**
 * dateObj:{
 *    '2017-01-01':{
 *      name: '2017-01-01',
 *      date: memont(),
 *      year: 2017,
 *      month: 1,
 *      day: 1,
 *      weekday: 0,         周日
 *      week: 1,            第一周
 *      isHoliday: false,   是否假日
 *      festival: '',       节日,默认是农历, 法定节日 > 外国节日 > 农历
 *    }
 *
 * }
 */
export default {
  data(){
    return {
      show: true,
      dateObj: {},               // 日期对象
      dateArray: [],             // 日期数组
      transitionType: 'calendar-item-right',     // 动画方式, calendar-item-left/calendar-item-right
    }
  },
  props: [
    'month',      // 月份的moment对象
  ],
  watch:{
    month: function(newValue, oldValue){
      if(newValue < oldValue){
        this.transitionType = 'calendar-item-left'
      }else{
        this.transitionType = 'calendar-item-right'
      }
      this.show = !this.show
    }
  },
  created(){
    this.initData()
  },
  methods:{
    initData(){
      console.log('month: ', this.month)
      var dateObj = {}
      var dateArray = []

      var startDay = moment(this.month).startOf('month')
      var endDay = moment(this.month).endOf('month')



      while (startDay <= endDay) {
        var day = startDay.day();
        dateObj[startDay.format('l')] = {
          name: startDay.format('l'),
          date: moment(startDay),
          weekday: day,
          year: startDay.year(),
          month: startDay.month() + 1,
          day: startDay.date(),
          festival: this.initFestival(moment(startDay)),
          isHoliday: this.initHoliday(moment(startDay))
        }

        if(day == 0 || startDay.format('l') == this.month.format('l')){
          var weekArray = new Array(7).fill('')
          dateArray.push(weekArray);
        }

        weekArray[day] = startDay.format('l')

        startDay.add(1, 'd')
      }

      var monthObj = moment(this.month)

      this.dateObj = dateObj
      this.dateArray = dateArray

    },
    /**
     * 初始化节日
     */
    initFestival(date){
      var chineseDate = chinese.solar2lunar(date.year(), date.month() + 1, date.date())
      var festivalName = chineseDate.IDayCn
      festivalName = festival[date.format('l')] || festivalName

      return festivalName
    },
    /**
     * 初始化假日
     */
    initHoliday(date){
      var isHoliday = false
      var day = date.day()
      if(day == 6 || day == 0){
        isHoliday = true
      }
      if(holiday[date.format('l')] != undefined){
        isHoliday = holiday[date.format('l')]
      }
      return isHoliday

    },
    getCurrentDay(key){
      return this.dateObj[key]? this.dateObj[key].day: ''
    },
    afterLeave(){
      this.initData();
      this.show = !this.show;
    },
    tdClass(key){
      if(this.dateObj[key] && this.dateObj[key].isHoliday){
        return 'calendar-td calendar-td-holiday'
      }else{
        return 'calendar-td'
      }
    }


  },
  computed: {

  }
}
